package Checkers; 
 

public class Move
{
    public Coord[] moves;
    public int n; //array size, 1 move after the last move
    public int m; //current move number, when reading off move list
    
    //create a blank move
    public Move()
    {
        n=0;
        m=1;
        moves = new Coord[13];//13 is max possible move combo
    }
    
    //create a move with an initial start and end location
    public Move(Coord c, Coord d)
    {
        n=2;
        m=1;
        moves=new Coord[13];
        moves[0]=new Coord(c);
        moves[1]=new Coord(d);
    }

    public Move(int x1,int y1, int x2,int y2)
    {
        n=2;
        m=1;
        moves=new Coord[13];
        moves[0]=new Coord(x1,y1);
        moves[1]=new Coord(x2,y2);
    }
    
    public void addCoord(Coord c)
    {
        moves[n]=new Coord(c);
        n++;
    }
    
    public void addCoord(int x,int y)
    {
        moves[n]=new Coord(x,y);
        n++;
    }
    
    //returns next move when running through list of moves
    public Coord next()
    {
        m++;
        return moves[m-1];
    }
    
    //returns the starting position of the piece
    public Coord start()
    {
        return moves[0];
    }
    
    //returns the end position of the piece
    public Coord end()
    {
        return moves[n-1];
    }
    
    //total number of jumps (should always be 2 <= n <= 13)
    public int size()
    {
        return n;
    }
    
    public int currentPos()
    {
        return m;
    }
    
    //number of jumps until the last jump
    public int remainingCoords()
    {
        return n-m;
    }
    
    //used when reading moves 1 by 1, tells you when you're done
    public boolean isEmpty()
    {
        return m==n;
    }
    
    //move type:
    //0 = empty, no moves
    //-1 = only 1 coordinate, invalid move
    //1 = single move, no jump
    //2 = single move, jumped a piece
    //3 = multiple jumps
    public int getType()
    {
        if(n==0)
            return 0;
        if(n==2)
        {
            if (Math.abs(moves[1].x-moves[0].x)==1 && Math.abs(moves[1].y-moves[0].y)==1)
                return 1;
            else
                return 2;
        }
        if(n>2)
            return 3;
        return -1;
    }
    
    
    public Move getCopy() {
        
        Move copy = new Move();
        
        for (int i = 0; i < n; ++i){
            
            copy.addCoord(moves[i].x, moves[i].y);
        }
        
        copy.n = this.n;
        return copy;
    }
    
    public Coord get(int index)
    {
        if (index<n)
            return moves[index];
        else
            return null;
    }
    
    public boolean equals(Move m)
    {
        boolean match=true;
        for(int i=0;i<n;i++)
        {
            if (!moves[i].equals(m.get(i)))
            {
                match=false;
                break;
            }
        }
        return match;
    }
    
    public String toString()
    {
        String k="Move: ";
        for(int i=0;i<n;i++)
        {
            if (i!=0)
                k+=" to ";
            k+=moves[i];
        }
        return k;
    }

    public String toServerString()
    {
        String k = "";
        for(int i=0;i<n;i++)
        {
            if (i!=0)
                k+=":";
            k+=moves[i];
        }
        return k;
    }
    
    public boolean coordExists( Coord c ) {
    	
    	for ( int i = 0; i < n; ++i ) {
    		
    		if ( c.equals(moves[i]) ) 
    			return true;
    	}
    	
    	return false;
    }
}
